{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Learn Matplotlib Basic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# show in notebook, and not block, # don't need call plt.show()\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pylab as plt\n",
    "\n",
    "# http://pandas.pydata.org/pandas-docs/version/0.13/visualization.html\n",
    "\n",
    "# datetime timeseries display\n",
    "# https://blog.csdn.net/fortware/article/details/51934814\n",
    "# https://matplotlib.org/gallery/api/date.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# figure and show\n",
    "fig = plt.figure(figsize=(2, 2))\n",
    "ax = fig.add_subplot(111)\n",
    "\n",
    "# set x/y scale range\n",
    "ax.set_xlim([0,4])\n",
    "ax.set_ylim([0,8])\n",
    "\n",
    "# grid\n",
    "ax.grid(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置子图之间的间距\n",
    "plt.tight_layout(pad = 3, h_pad=0.5, w_pad=0.5)\n",
    "\n",
    "# 为了防止Jupyter notebook有时候Double plot, 在plot函数的后面加分号;\n",
    "plt.plot([1,2,3,4,5]);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(1, 1))\n",
    "ax = fig.add_subplot(111)\n",
    "ax.set_title('axis off')\n",
    "ax.axis('off')\n",
    "ax.plot([1,2,3,4,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(3, 3))\n",
    "ax = fig.add_subplot(111)\n",
    "# not my disired result\n",
    "# ax.set_visible(Flase)\n",
    "\n",
    "# set axis label\n",
    "ax.set_xlabel('X')\n",
    "ax.set_ylabel('Y')\n",
    "\n",
    "ax.plot(np.random.randn(1000).cumsum(), label='line0')\n",
    "\n",
    "# set axis scale and scale label\n",
    "ax.set_xticks(range(0,1000,100))\n",
    "ax.set_xticklabels(list('abcdefghij'))\n",
    "# ax.set_xticklabels([])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get current axis\n",
    "ax = plt.gca()\n",
    "# y axis visible\n",
    "ax.axes.get_yaxis().set_visible(False)\n",
    "# x axis visible\n",
    "ax.axes.get_xaxis().set_visible(False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# another set about axis and ticks\n",
    "# plt.xticks([]) and plt.yticks([])\n",
    "# plt.xlim([]) and plt.ylim([])\n",
    "# plt.xlabel() and plt.ylabel()\n",
    "# plt.axis('off')\n",
    "# plt.tile()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# hide the spines (轴,脊骨)\n",
    "fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(10, 2))\n",
    "# clear the ticks on axis of x,y \n",
    "axes[0].set_xticks([])\n",
    "axes[0].set_yticks([])\n",
    "\n",
    "axes[1].xaxis.set_ticks_position('top')\n",
    "axes[1].spines['bottom'].set_position(('data', 0.5))\n",
    "axes[1].yaxis.set_ticks_position('right')\n",
    "axes[1].spines['left'].set_position(('data', 0.5))\n",
    "\n",
    "axes[2].spines['right'].set_color('none')\n",
    "axes[2].spines['top'].set_color('none')\n",
    "axes[2].spines['bottom'].set_position(('data', 0.5))\n",
    "\n",
    "axes[2].set_yticklabels(list('abcdef'))\n",
    "axes[2].set_xticks([])\n",
    "# set the around size between subplots\n",
    "plt.subplots_adjust(wspace=0.5, hspace=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig.set_figheight(10)\n",
    "fig.set_figwidth(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 在Cell中复用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(1, 1, figsize=(4,4))\n",
    "ax.scatter([1],[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 另一个cell\n",
    "ax.scatter([2],[2])\n",
    "fig"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
